package com.dingwei.lesyndrome.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.MyReleaseListAdapter;
import com.dingwei.lesyndrome.bean.MyReleaseBean;
import com.dingwei.lesyndrome.mvp.presenter.MyReleaseFragmentPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.ui.activity.CircleDetailsActivity;
import com.dingwei.lesyndrome.ui.activity.VideoPlayerActivity;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.L;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 我的发布 - fragment
 */

public class MyReleaseFragment extends LazyLoadFragment implements Contract.MyReleaseFragmentView {
    MyReleaseFragmentPresenter gamCircleFragmentPresenter = new MyReleaseFragmentPresenter(this);
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    private String id;
    MyReleaseListAdapter adapter;


    //接收数据
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        id = getArguments().getString("id");
        L.e("id = " + getArguments().getString("id"));
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_gam_circle;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();

    }

    private void initView() {
        adapter = new MyReleaseListAdapter(BaseActivity.context);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));
        mRecyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                gamCircleFragmentPresenter.initData(id, page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                gamCircleFragmentPresenter.initData(id, page, true, false);
            }
        });


        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                MyReleaseBean.DataBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, CircleDetailsActivity.class);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });


        adapter.xxInterfaceListener(new MyReleaseListAdapter.clickInterFace() {
            @Override
            public void onLike(int position) {
                MyReleaseBean.DataBean bean = adapter.getItem(position);
                gamCircleFragmentPresenter.like(bean.id,position);
            }

            @Override
            public void onComment(int position) {

            }

            @Override
            public void onPlay(int position) {
                MyReleaseBean.DataBean bean  = adapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, VideoPlayerActivity.class);
                intent.putExtra("url", bean.video);
                intent.putExtra("face",  bean.vimg);
                startActivity(intent);
            }

            @Override
            public void onDelete(int position) {
                MyReleaseBean.DataBean bean = adapter.getItem(position);
                gamCircleFragmentPresenter.delete(bean.id,position);
            }
        });
    }

    @Override
    protected void lazyLoad() {
        refreshLayout.autoRefresh();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData("fail");
    }

    @Override
    public void onRefresh(MyReleaseBean bean) {
        refreshLayout.finishRefresh();
        adapter.clear();
        adapter.addAll(bean.data);
        showData("success");
    }

    @Override
    public void onLoadMore(MyReleaseBean bean) {
        refreshLayout.finishLoadmore();
        adapter.addAll(bean.data);
        showData("success");
    }

    @Override
    public void likeSuccess(int positon) {
        MyReleaseBean.DataBean bean = adapter.getItem(positon);
        bean.isAgree = !bean.isAgree;
        adapter.notifyItemChanged(positon,bean);
    }

    @Override
    public void deleteSuccess(int positon) {
        adapter.remove(positon);
        showData("success");
    }

    @OnClick(R.id.no_net_btn)
    public void onViewClicked() {
        page = 1 ;
        gamCircleFragmentPresenter.initData(id,page,true,true);
    }

    public  void Refesh(){
        page = 1 ;
        gamCircleFragmentPresenter.initData(id,page,true,true);
    }


}
